package objects.visualization;

import base.Graphics;
import static base.Main.menu;
import static base.Main.showMenu;
import java.util.List;
import menu.MenuItem;
import objects.DataObject;
import objects.GruObject;
import objects.slots.Slot;
import org.lwjgl.input.Mouse;

public class Block {
	public static Block selected = null;
	public static boolean drawSelected = false;
	public static Slot selectedSlot;
	
	public int x, y, width, height;
	public DataObject object;
	public Slot slot;
	
	public static int defaultMargin = 8;

	public Block() {
	}
	
	public Block( DataObject object ) {
		this.object = object;
	}
	
	public Block( Slot slot ) {
		this.slot = slot;
	}
	
	public void draw( int drawingX, int drawingY ) {
		if( drawSelected ) {
			Graphics.setColor( 0.6f, 0.6f, 1f );
			if( this == selected ) Graphics.drawRectangle( drawingX + x - 2, drawingY + y - 2, width + 4, height + 4 );
		} else {
			if( collides( Mouse.getX(), 768 - Mouse.getY(), 3, 3, drawingX + x + width / 2, drawingY + y + height / 2, width, height ) ) selected = this;
			Graphics.setColor( 0.5f, 0.5f, 1f );
		}
		Graphics.drawRectangle( drawingX + x, drawingY + y, width, height );
	}
	
	public void update() {
		
	}

	public boolean collides( int x1, int y1, int width1, int height1, int x2, int y2, int width2, int height2 ) {
		return 2 * Math.abs( x1 - x2 ) < width1 + width2 && 2 * Math.abs( y1 - y2 ) < height1 + height2;
	}

	public Block setSlot( Slot slot, GruObject method ) {
		this.slot = slot;
		slot.method = method;
		return this;
	}
	
	public void select() {
		if( object != null ) {
			object.select();
		} else {
			List<MenuItem> list = menu.get( null );
			for( MenuItem item : list ) item.object = object;
			Block.selectedSlot = Block.selected.slot;
			showMenu( list );
		}
	}
}